package com.qianda.qdminiapp.service.impl;

import com.qianda.qdminiapp.common.constant.CommonConstant;
import com.qianda.qdminiapp.domain.model.account.QqdAccountTrade;
import com.qianda.qdminiapp.dao.account.QqdAccountTradeDao;
import com.qianda.qdminiapp.domain.model.order.QqdOrderInfo;
import com.qianda.qdminiapp.service.QqdAccountTradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 账户交易明细 服务实现类
 * </p>
 *
 * @author cw
 * @since 2019-08-14
 */
@Service
@Transactional
public class QqdAccountTradeServiceImpl extends ServiceImpl<QqdAccountTradeDao, QqdAccountTrade> implements QqdAccountTradeService {

    @Autowired
    private QqdAccountTradeDao qqdAccountTradeDao;

    @Override
    @Transactional
    public void insert(QqdOrderInfo qqdOrderInfo,Long userId) {
        //新增一条交易明细
        QqdAccountTrade qqdAccountTrade = new QqdAccountTrade();
        qqdAccountTrade.setUserId(userId);
        //  qqdAccountTrade.setAccountId(user.getAccountId());

        int tradeType = 0;
        int tradeState = 0;
        if(qqdOrderInfo.getOrderType() == CommonConstant.QqdOrderType.QQD_ORDER_TYPE_BUY_TOWEL){
            tradeType = CommonConstant.TradeType.TRADE_TYPE_BUY_TOWEL;
            tradeState = CommonConstant.TradeState.TRADE_STATE_OUT;
        }else if (qqdOrderInfo.getOrderType() == CommonConstant.QqdOrderType.QQD_ORDER_TYPE_CHARGE){
            tradeType = CommonConstant.TradeType.TRADE_TYPE_WALLET;
            tradeState = CommonConstant.TradeState.TRADE_STATE_IN;
        }

        qqdAccountTrade.setTradeType(tradeType);
        qqdAccountTrade.setTradeState(tradeState);
        qqdAccountTrade.setTradeMode(CommonConstant.TradeMode.TRADE_MODEL_WX);
        qqdAccountTrade.setTradeTime(LocalDateTime.now());
        //  qqdAccountTrade.setTradeBeforeAmt(qqdAccount.getBalance());
        qqdAccountTrade.setTradeAmt(qqdOrderInfo.getOrderAmt());
        qqdAccountTrade.setStatus(CommonConstant.Status.STATUS_NORMAL);
        qqdAccountTrade.setCreateTime(LocalDateTime.now());
        qqdAccountTradeDao.insert(qqdAccountTrade);
    }
}
